<template>
  <div class="content">
    <div class="category">
      <div v-for="item in categories"
      :class="{'category__item': true, 'category__item--active': currentTab === item.tab}"
      :key="item.name"
      @click="() => handleTabClick(item.tab)"
      >
      {{item.name}}
      </div>
    </div>
    <div class="product">
      <div
      class="product__item"
      v-for="(item) in list"
      :key="item._id"
      >
          <img :src="item.imgUrl" class="product__item__img" />
          <div class="product__item__detail">
              <h4 class="product__item__title">{{item.name}}</h4>
              <p class="product__item__sales">月售 {{item.sales}} 件</p>
              <p class="product__item__price">
                <span class="product__item__yen">&yen;</span>{{item.price}}
                <span class="product__item__origin">&yen;{{item.oldPrice}}</span>
              </p>
          </div>
          <div class="product__number">
            <span
            class="product__number__minus iconfont"
            @click="() => { changeCartItem(shopId, item._id, item, -1, shopName) }"
            >&#xe701;</span>
            {{getProductCartCount(shopId, item._id)}}
            <span
            class="product__number__plus iconfont"
            @click="() => { changeCartItem(shopId, item._id, item, 1, shopName) }"
            >&#xe7ba;</span>
          </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reactive, ref, toRefs, watchEffect } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { get } from '../../utils/request'
import { useCommonCartEffect } from '../../effects/cartEffects'

const categories = [
  { name: '全部商品', tab: 'all' },
  { name: '秒杀', tab: 'seckill' },
  { name: '新鲜水果', tab: 'fruit' }
]

// tab切换相关的逻辑
const useTabEffect = () => {
  const currentTab = ref(categories[0].tab)
  const handleTabClick = (tab) => {
    currentTab.value = tab
  }
  return { currentTab, handleTabClick }
}

// 列表数据相关逻辑
const useCurrentListEffect = (currentTab, shopId) => {
  const content = reactive({ list: [] })
  const getContentData = async () => {
    const result = await get(`/api/shop/${shopId}/products`, {
      tab: currentTab.value
    })
    if (result?.errno === 0 && result?.data?.length) {
      content.list = result.data
    }
  }

  // 当首次加载以及监听的依赖发生变化的时候执行
  watchEffect(() => { getContentData() })
  const { list } = toRefs(content)
  return { list }
}

// 处理购物车相关逻辑
const useCartEffect = () => {
  const store = useStore()
  const { cartList, changeCartItemInfo } = useCommonCartEffect() // 购物车相关逻辑1 处理产品相关信息
  const changeShopName = (shopId, shopName) => { // 购物车相关逻辑2 加入店铺名
    store.commit('changeShopName', { shopId, shopName })
  }
  const changeCartItem = (shopId, productId, item, num, shopName) => { // 调用 购物车相关逻辑1, 2
    changeCartItemInfo(shopId, productId, item, num)
    changeShopName(shopId, shopName)
  }
  const getProductCartCount = (shopId, produectId) => {
    return cartList?.[shopId]?.productList?.[produectId]?.count || 0
  }

  return { cartList, changeCartItem, getProductCartCount }
}

export default {
  name: 'Content',
  props: ['shopName'],
  setup () {
    const route = useRoute()
    const shopId = route.params.id
    const { currentTab, handleTabClick } = useTabEffect() // tab切换相关的逻辑
    const { list } = useCurrentListEffect(currentTab, shopId) // 列表数据相关逻辑
    const { changeCartItem, cartList, getProductCartCount } = useCartEffect() // 购物车相关逻辑

    return { shopId, categories, list, currentTab, handleTabClick, changeCartItem, cartList, getProductCartCount }
  }
}
</script>

<style lang="scss" scoped>
@import '../../style/viriables.scss';
@import '../../style/mixins.scss';
.content {
    display: flex;
    position: absolute;
    left: 0;
    right: 0;
    top: 1.5rem;
    bottom: .5rem;
}
.category {
    overflow-y: scroll;
    height: 100%;
    width: .76rem;
    background: $search-bgColor;
    &__item {
        line-height: .4rem;
        text-align: center;
        color: $content-fontcolor;
        font-size: .14rem;
        &--active {
            background: $bgColor;
        }
    }
}
.product {
    overflow-y: scroll;
    flex: 1;
    &__item {
        display: flex;
        position: relative;
        padding: .12rem 0 ;
        margin: 0 .16rem;
        border-bottom: .01rem solid $content-bgColor;
        &__detail {
            overflow: hidden;
        }
        &__img {
            height: .68rem;
            width: .68rem;
            margin-right: .16rem;
        }
        &__title {
            margin: 0;
            line-height: .2rem;
            font-size: .14rem;
            color: $content-fontcolor;
            @include ellipsis;
        }
        &__sales {
            margin: .06rem 0;
            line-height: .16rem;
            font-size: .12rem;
            color: $content-fontcolor;
        }
        &__price {
            margin: 0;
            line-height: .2rem;
            font-size: .14rem;
            color: $hightlight-fontColor;
        }
        &__yen {
            font-size: .12rem;
        }
        &__origin{
            margin-left: .06rem;
            line-height: .2rem;
            font-size: .12rem;
            color: $light-fontColor;
            text-decoration: line-through;
        }
        .product__number {
            position: absolute;
            right: .02rem;
            bottom: .12rem;
            &__minus, &__plus {
                position: relative;
                top: .023rem;
                font-size: .2rem;
            }
            &__minus {
                color: $medium-fontColor;
                margin-right: .07rem;
            }
            &__plus {
                color: $btn-bgColor;
                margin-left: .07rem;
            }
        }
    }
}
</style>
